package com.romotc.qqrobot.webqq.qqInterfaceCall.dataProcessor;

import java.util.Iterator;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.romotc.qqrobot.bean.QQLogedin;
import com.romotc.scache.RequestSpy;
/**
 * 获取验证码接口处理类
 * 设置当前登录QQ的cookie信息，将验证码图片保存到本地
 * @author Administrator
 *
 */
public class Getimage_Processor implements DateProcessor {

	public static final String validatorImageFolder = "./validImage/";
	
	public static String Key_VerifySession = "verifysession";

	@Override
	public JSONObject processResponse(QQLogedin loginQQ, 
			RequestSpy rqSpy, JSONObject reqDataObj) {
		// TODO Auto-generated method stub

		QQLogedin curQQ = QQLogedin.getCurQqLogedIn(rqSpy);
		JSONObject resCookie = rqSpy.getResCookiesJson();
		
		if(curQQ != null){
			//设置当前Qq cookie中的verifysession信息 RomUnDo
			//在这里设置 log的方式是起线程的 有可能滞后于这里
			//RomUnDo
			String cookieStr = curQQ.getCookieString();
			JSONObject cookie = RequestSpy.parseCookieJsonFromStr(cookieStr);
			try {
				cookie.put("verifysession", resCookie.get("verifysession"));
				String cookieString = RequestSpy.parseCookieJsonToStr(cookie);
//				curQQ.setCookieString(cookieString);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		//保存图片 RomUnDo 并设置到res JsonObject中
		
		String key = null;
		Object value = null;
		Iterator<Object> iterator = resCookie.keys();
		while(iterator.hasNext())
		{
			key = (String)iterator.next();
			try {
				value = resCookie.get(key);
				reqDataObj.put(key, value);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return reqDataObj;
		
	}

	private static Getimage_Processor instance = null;
	public static synchronized DateProcessor getInstance() {
		if(instance == null)
			instance = new Getimage_Processor();
		
		return instance;
	}

}
